#!/bin/bash
# Realiza um comando svn para backup
BACKUPDIR='/var/backups/svn'
SVNDIR=/svn
BASEDIR=/var
PROJECTDIR=$BASEDIR$SVNDIR
SVNCOMAND='svnadmin -q dump'
EXT='.svndump'
CMDDESTINO="sudo scp $a backup@gurupi:/var/backups/svn"
DAYS=3
LOGFILE='backups.log'

#find $PROJECTDIR -maxdepth 1 -type d| grep -v $SVNDIR$ | while read a; do echo -e $a '\t'- $BACKUPDIR/$(echo $a |awk -F/ '{print $4}')-$(date +%Y%m%d%H%M).svndump; done

echo -e date +%Y%m%d%H%M |tee -a $BACKUPDIR/$LOGFILE
echo -e "Iniciando backup dos projetos no $PROJECTDIR" |tee -a $BACKUPDIR/$LOGFILE 
find $PROJECTDIR -maxdepth 1 -type d| grep -v $SVNDIR$ | while read a; do $SVNCOMAND $a > $BACKUPDIR/$(echo $a |awk -F/ '{print $4}')-$(date +%Y%m%d%H%M)$EXT 2|tee -a $BACKUPDIR/$LOGFILE; done

echo -e "executando envio do backup \'$CMDDESTINO\'" |tee -a $BACKUPDIR/$LOGFILE
find $BACKUPDIR -type f -name "*$EXT" -cmin -20 |while read a; do scp $a $SVNCOMAND; done |tee -a $BACKUPDIR/$LOGFILE

echo -e "removendo arquivos superiores a $DAYS dias" |tee -a $BACKUPDIR/$LOGFILE
find $BACKUPDIR -type f -name "*$EXT" -ctime +$DAYS -exec rm -vf {} \; |tee -a $BACKUPDIR/$LOGFILE
